package com.ssmTmp.api.util;

import com.ssmTmp.api.constant.CommonsConstant;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.*;


/**
 * 用户信息
 */
@Component
public class UserManage {

    public static final String USER_ID="USER_ID";
    public static final String USERNAME="USERNAME";
    public static final String HSJG_DM="HSJG_DM";
    public static final String USER="user";
    private static final ThreadLocal<String> USER_TOKEN = new ThreadLocal<String>();
    public static final String DEPT_ID = "DEPT_ID";

    public  void setUserToken(String userToken){
        USER_TOKEN.set(userToken);
    }
    public static StringRedisTemplate stringRedisTemplate;

    @Autowired(required = true)
    public  void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        UserManage.stringRedisTemplate = stringRedisTemplate;
    }
    /**
     * 获取当前登录用户 ID
     * @return
     */
    public  static String getUserId(){

        if(StringUtils.isBlank(USER_TOKEN.get())){
            return "362";
        }
        HashMap<String,Object> map = getUserByToken(USER_TOKEN.get());
        HashMap<String,String> userMap =   JSON.parseObject( JSON.toJSONString(map.get(USER)) ,HashMap.class);
        return userMap.get(USER_ID);
    }

    /**
     * 获取当前登录用户 部门ID
     * @return
     */
    public  static String getDeptId(){
        if(StringUtils.isBlank(USER_TOKEN.get())){
            return "100745";
        }
        HashMap<String,Object> map = getUserByToken(USER_TOKEN.get());
        HashMap<String,String> userMap =   JSON.parseObject( JSON.toJSONString(map.get(USER)) ,HashMap.class);
        return userMap.get(DEPT_ID);
    }

    /**
     * 获取当前登录用户 海事机构ID
     * @return
     */
    public  static String getHsjgId(){
        if(StringUtils.isBlank(USER_TOKEN.get())){
            return "070000";
        }
        HashMap<String,Object> map = getUserByToken(USER_TOKEN.get());
        HashMap<String,String> userMap =   JSON.parseObject( JSON.toJSONString(map.get(USER)) ,HashMap.class);
        return userMap.get(HSJG_DM);
    }

    /**
     * 获取用户个人指定信息
     * @param
     * @return
     */
    public static String getUserInfo(String key){
        String token=USER_TOKEN.get();
        if(StringUtils.isBlank(token)||StringUtils.isBlank(key)){
            return null;
        }

        HashMap<String,Object> map = getUserByToken(token);


        HashMap<String,String> userMap =   JSON.parseObject( JSON.toJSONString(map.get(USER)) ,HashMap.class);

        return userMap.get(key);

    }
    /**
     * 获取用户个人所有信息
     * @param
     * @return
     */
    public static HashMap<String, String> getUser(){
        String token=USER_TOKEN.get();

//        token="TIHJA7439AfrKx";
        if(StringUtils.isBlank(token)){
            return null;
        }

        HashMap<String,Object> map = getUserByToken(token);


        HashMap<String,String> userMap =   JSON.parseObject( JSON.toJSONString(map.get(USER)) ,HashMap.class);
        return userMap;

    }


    /**
     * 获取用户所有信息
     * @param token
     * @return
     */
    public static HashMap getUserByToken(String token){
        if(StringUtils.isBlank(token)){
            token="";
        }
        String t =  stringRedisTemplate.opsForValue().get(token);
        HashMap<String,Object> map = JSON.parseObject(t,HashMap.class);
        return map;
    }

    public static String getUsername() {
        String username = "";
        System.out.println(USER_TOKEN.get());
        HashMap userByToken = getUserByToken(USER_TOKEN.get());

        if (userByToken.get("user") instanceof Map) {
            Map<String, String> map = (Map<String, String>) userByToken.get("user");
            username = map.get("USERNAME");
        }
        return username;
    }
    /*得到用户的所有角色id*/
    public static List<String> getRoleList() {
        System.out.println(USER_TOKEN.get());
        HashMap userByToken = getUserByToken(USER_TOKEN.get());
        List<Map<String, String>> list=(List<Map<String, String>>)userByToken.get("roleList");
        List<String> roleList=new ArrayList<>();
        list.forEach(stringStringMap -> {
            roleList.add(stringStringMap.get("ROLE_ID"));
        });
        return roleList;
    }

    /*判断当前用户是否存在此角色 多个角色用,隔开*/
    public static boolean whetherHave(String roleIds) {
        List<String> roles= Arrays.asList(roleIds.split(CommonsConstant.COMMA_SYMBOL));
        System.out.println(USER_TOKEN.get());
        HashMap userByToken = getUserByToken(USER_TOKEN.get());
        List<Map<String, String>> list=(List<Map<String, String>>)userByToken.get("roleList");
        for (Map<String, String> map : list) {
            if (roles.contains(map.get("ROLE_ID"))){
                return true;
            }
        }
        return false;
    }


}
